﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using System.Xml;
public partial class Dom操作_HTmlToXML : System.Web.UI.Page
{

    /// <summary>
    /// 数据操作通用方法
    /// </summary>
    Tong.XmlHttp.GetWebData GetData = new Tong.XmlHttp.GetWebData();
    protected void Page_Load(object sender, EventArgs e)
    {
        //test how to use Array
         
    }

    //执行
    protected void Button1_Click(object sender, EventArgs e)
    {
       // Response.Write(Tong.WorkXml.HtmlToXML.XmlConvert(FormValidation1.Text));
       // FormValidation1.Text = Tong.WorkXml.HtmlToXML.XmlConvert(FormValidation1.Text);
      FormValidation1.Text = Tong.WorkXml.HtmlToXML.HTMLConvert(FormValidation1.Text);
     // Response.Write("<data>" + FormValidation1.Text + "</data>");
     // Response.Charset = "Utf-8";
     // Response.ContentType = "text/xml";
    //start ParSer XML file Contont
    //开始分析MXL文件内容
      Response.Write(XmlParser("<data>" + FormValidation1.Text + "</data>")); ;

     Response.End();
        
    }


    /// <summary>
    /// 分析XML文件
    /// 
    /// </summary>
    /// <param name="xmlString"></param>
    /// <returns></returns>
    public string XmlParser(string xmlString)
    {
        //[\u4e00-\u9fa5\d\w１２３４５６７８９～！·＃￥％……—＊（）——＋／”》“‘’；。、？，：…《]+[\u4e00-\u9fa5１２３４５６７８９～！·＃￥％……—＊（）——＋／”》“‘’；。、？，：…《]
        //More Array Value
        string  mainContont ="";//文章内容
        XmlDataDocument MyXml = new XmlDataDocument();
        MyXml.LoadXml(xmlString);
        //try
        //{
            
            mainContont = MyXml.DocumentElement.GetElementsByTagName("body")[0].OuterXml;
            //如果不能得到body内容   
            if(mainContont.Length<=100)
            {
                mainContont = xmlString;
            }
            System.Text.RegularExpressions.MatchCollection MC = System.Text.RegularExpressions.Regex.Matches(mainContont, @"[\u4e00-\u9fa5\d\w１２３４５６７８９～！!·＃￥％……—＊（）——＋／”》“‘’，；。、？，：…《]+[\u4e00-\u9fa5１２３４５６７８９～！!·＃￥％……—＊（!）——＋／”》“‘，’\r\n；。、？，：…《]");

         
        
        int j = 0;
            //创建 一个数组用于保存 从第一个开始的特定数据到下一个特定数据出面的距离
            System.Collections.Generic.List<AppOvt> ppu = new System.Collections.Generic.List<AppOvt>();//初始化对像
            System.Collections.Generic.List<AppOvt> ppu_Old = new System.Collections.Generic.List<AppOvt>();//初始化对像用于保存没有排序的数据

            int olk = 0; //保存的距离数据
            int Isone = 0;//如果是第一个
            //冒泡排序所有到的参数
            int tempint = 0;//currpant line
            int tempspace = 0;//space 
            string tempcont = "";//初始化内容
            //最小距离值
            int MinSpaceValue = 11;//初始化为放弃的值
            
            while (j < MC.Count)
            {
                 //Response.Write(MC[j].Value+" "+ ((MC[j].Value.Length >= 50) ? "1" : "0")+"<br/>");
                //从开始第一个1 到下一个1 中间格的数据
                if (MC[j].Value.Length >= 50)//20个不对
                {
                    if (Isone == 0)
                    {
                        Isone = j;//如果是第一个数据
                        olk = j;

                        //当前位置,上一个的 space
                        AppOvt oou = new AppOvt();
                        oou.currrow = j;
                        oou.space = Isone;
                        oou.cont = MC[j].Value;
                        ppu.Add(oou);


                        //初始化原始
                        AppOvt ccc = new AppOvt();
                        ccc.currrow = j;
                        ccc.space = olk;
                        ccc.cont = MC[j].Value;
                        ppu_Old.Add(ccc);

                    }
                    else//如果不是第一个的话
                    {
                        olk = j - olk;
                        AppOvt oou = new AppOvt();
                        oou.currrow = j;
                        oou.space = olk;
                        oou.cont = MC[j].Value;
                        ppu.Add(oou);

                        //初始化原始
                        AppOvt ccc = new AppOvt();
                        ccc.currrow = j;
                        ccc.space = olk;
                        ccc.cont = MC[j].Value;
                        ppu_Old.Add(ccc);

                        olk = j;
                    }
                }
                j++;
            }


            //xml line count
            //得到所有汉字的东东
            //如果汉字多则可能性大

            //用冒泡把序排下 方便下面得到数据
            for (int ii = 0; ii < ppu.Count; ii++)
            {

                for (int jj = 0; jj < ppu.Count - 1; jj++)
                {
                    //如果当前比下一个的数大
                    //desc for ppu
                    if (ppu[jj].space > ppu[jj + 1].space && ppu[jj].space != 0)
                    {
                        tempspace = ppu[jj].space;
                        tempcont = ppu[jj].cont;
                        tempint = ppu[jj].currrow;

                        ppu[jj].space = ppu[jj + 1].space;
                        ppu[jj].currrow = ppu[jj + 1].currrow;
                        ppu[jj].cont = ppu[jj + 1].cont;

                        ppu[jj + 1].space = tempspace;
                        ppu[jj + 1].currrow = tempint;
                        ppu[jj + 1].cont = tempcont;
                    }

                }
            }



            //foreach (AppOvt oo in ppu)
            //{
            //    Response.Write("当前数" + oo.currrow);
            //    Response.Write("和上一个的space" + oo.space + "<br/>");
            //}

            //动态创建正则表达式
            string Regexstr = "";
            //得到最小的 距离值 当最小的距离值大于 在2以内的时候动态创建 正则表达式
            //如是在2以外的话，最小距离值 在10以内就动态创建正则表达式 在10以外就放弃
            //并且，第二个最小值不能和第一个最小值的 距离 超过10
            //the low code get minvalue for space
            if (ppu.Count > 1)
            {
                //如果上一行到下一行的值为0的话那么 这一行为第一持程序要移除
                if (ppu_Old[0].space == 0)
                {
                    ppu_Old.RemoveAt(0);
                    ppu.RemoveAt(0);
                }

                MinSpaceValue = ppu[0].space;//get min space value


                j = 0;
                if (MinSpaceValue <= 10 && ppu[1].space - MinSpaceValue < 10)//start action Regex string
                {
                    //动态创建 正则表达式 提取 第一个 距离 的最小值，和第二个 距离 的最小值
                    for (int ispace = 0; ispace < ppu_Old.Count; ispace++)
                    {

                        if ((ppu_Old[1].space - MinSpaceValue) == 0)
                        {
                            if (ppu_Old[ispace].space <= 1 && j < 4)//只提取两级
                            {
                                Regexstr += ppu_Old[ispace].cont + @"[\w\W]+";
                                j++;
                            }
                        }
                        else
                        {
                            if (ppu_Old[ispace].space <= (ppu_Old[1].space - MinSpaceValue) && j < 4)//只提取两级
                            {
                                Regexstr += ppu_Old[ispace].cont + @"[\w\W]+";
                                j++;
                            }
                        }

                        if (j >= 3)
                        {
                            break;
                        }
                    }

                }
            }

            try
            {
                Regexstr = Regexstr.Substring(0, Regexstr.Length - 7);
            }
            catch
            {

            }
            //Response.Write(Regexstr);

            string DocCont = "";//文档内容
            //开始得到内容
            //td,th,div,p,li,ul


            XmlNodeList Td = MyXml.DocumentElement.GetElementsByTagName("td");//用td
            XmlNodeList Th = MyXml.DocumentElement.GetElementsByTagName("th");
            XmlNodeList Div = MyXml.DocumentElement.GetElementsByTagName("div");
            XmlNodeList P = MyXml.DocumentElement.GetElementsByTagName("p");
            XmlNodeList li = MyXml.DocumentElement.GetElementsByTagName("li");
            XmlNodeList ul = MyXml.DocumentElement.GetElementsByTagName("ul");
            //Response.Write(DocCountTemp);

            //th
            try
            {

                for (int i = 0; i < Td.Count; i++)
                {
                    if (System.Text.RegularExpressions.Regex.Match(Td[i].OuterXml, Regexstr).Value.Length > 0)
                    {
                        DocCont = Td[i].OuterXml;
                        // break;
                    }
                }
            }
            catch { }
            //th
            try
            {

                if (DocCont == "")
                {

                    for (int i = 0; i < Th.Count; i++)
                    {
                        if (System.Text.RegularExpressions.Regex.Match(Th[i].OuterXml, Regexstr).Value.Length > 0)
                        {
                            DocCont = Th[i].OuterXml;
                        }
                    }
                }
            }
            catch
            {

            }
            //div
            try
            {
                if (DocCont == "")
                {

                    for (int i = 0; i < Div.Count; i++)
                    {
                        if (System.Text.RegularExpressions.Regex.Match(Div[i].OuterXml, Regexstr).Value.Length > 0)
                        {
                            DocCont = Div[i].OuterXml;
                        }
                    }
                }
            }
            catch
            {

            }





            return DocCont;
        //}
        //catch
        //{
        //    return "can not read file";
        //}
    }

    

    /// <summary>
    /// 输入网址测试
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Button2_Click(object sender, EventArgs e)
    {
        string cont = GetData.GetWebDate(TextBox1.Text, "UTF-8");

        if (cont.Length > 0)
        {


            if (System.Text.RegularExpressions.Regex.Match(cont, @"�����").Value.Length == 0)
            {
                cont = XmlParser("<data>" + Tong.WorkXml.HtmlToXML.HTMLConvert(cont) + "</data>");
                
            }
            else
            {
                Response.Write("使用utf-8取得不成功<br/>改为GB2312");
                cont = XmlParser("<data>" + Tong.WorkXml.HtmlToXML.HTMLConvert(GetData.GetWebDate(TextBox1.Text, "GB2312")) + "</data>");

            }
            
            Response.Write(cont);
        }
        else
        {
            Response.Write("输入的网址不能直接访问");
        }
        Response.End();
    }
}


public class AppOvt
{

    public int currrow
    {

        get;
        set;
    }

    public int space { get; set; }

    public string cont { get; set; }
}


namespace Tong.WorkXml
{



   
}
